<template>
  <div class="app-container">
    <div class="Acolumn1">
      <div class="Bline-1">
        <el-tag type="success" class="Ctxt1">设备列表</el-tag>
      </div>
      <!-- <div class="Bline1">
        <el-descriptions
          direction="vertical"
          :column="4"
          style="width: 100%"
          border
        >
          <el-descriptions-item label="设备" label-align="right" align="center">
            121
          </el-descriptions-item>
          <el-descriptions-item label="报警" label-align="right" align="center">
            0
          </el-descriptions-item>
          <el-descriptions-item label="在线" label-align="right" align="center">
            121
          </el-descriptions-item>
          <el-descriptions-item label="离线" label-align="right" align="center">
            0
          </el-descriptions-item>
        </el-descriptions>
      </div> -->
      <div class="Bline2">
        <el-input
          v-model="searchName"
          placeholder="请输入名称"
          clearable
          prefix-icon="Search"
          style="margin-top: 2vh"
        />
      </div>
      <div class="Bline3">
        <el-tree
          :data="treeOptions"
          :props="{ label: 'label', children: 'children' }"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="treeRef"
          node-key="id"
          highlight-current
          default-expand-all
          @node-click="handleNodeClick"
        >
        </el-tree>
      </div>
    </div>
    <div class="Acolumn2">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入子名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['ruoyi-device:YYYYY:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['ruoyi-device:YYYYY:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['ruoyi-device:YYYYY:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['ruoyi-device:YYYYY:export']"
            >导出</el-button
          >
        </el-col> -->
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table
        v-loading="loading"
        :data="YYYYYPageList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="当前读表值" align="center" prop="AAA" />
        <el-table-column label="上次读表值" align="center" prop="BBB" />
        <el-table-column label="消耗量" align="center" prop="CCC" />
        <el-table-column label="状态" align="center" prop="stateResult">
          <template #default="scope">
            <div :style="scope.row.stateStyle">{{ scope.row.stateResult }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['ruoyi-device:YYYYY:edit']"
              >修改</el-button
            >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['ruoyi-device:YYYYY:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="YYYYYRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped lang="scss">
.app-container {
  display: flex;
  .Acolumn1 {
    border: 1px solid;
    width: 20%;
    height: 85vh;
    display: grid;
    .Bline-1 {
      height: 5vh;
      .Ctxt1 {
        margin-top: 10px;
        margin-left: 10px;
        font-weight: 400;
        font-size: 14px;
      }
    }
    // .Bline1 {
    //   height: 9vh;
    //   display: flex;
    // }
    .Bline2 {
      height: 7vh;
    }
    .Bline3 {
      overflow: scroll;
      height: 72vh;
    }
  }
  .Acolumn2 {
    margin-left: 1%;
    width: 79%;
    height: 85vh;
  }
}
</style>
<script setup>
/** 根据名称筛选部门树 */
const searchName = ref("");
import { watch } from "vue";
watch(searchName, (val) => {
  proxy.$refs["treeRef"].filter(val);
});
/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};

onMounted(() => {
  for (let i = 0; i < 50; i++) {
    let item1 = {
      id: i,
      label: "电表 1-" + i,
    };
    treeOptions.value.push(item1);
  }
  proxy.$refs["treeRef"].setCurrentKey(treeOptions.value[0].id);
});
const treeOptions = ref([]);

const YYYYYPageList = ref([]);
function handleNodeClick(data) {
  YYYYYPageList.value = YYYYYList.value.filter((item) => item.id == data.id);
}

const { proxy } = getCurrentInstance();

// import {
//   listXXXXX,
//   getXXXXX,
//   delXXXXX,
//   addXXXXX,
//   updateXXXXX,
// } from "@/api/ruoyi-device/YYYYY";

const YYYYYList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

onMounted(() => {
  getList();
});
/** 查询操作列表 */
function getList() {
  loading.value = true;
  // listXXXXX(queryParams.value).then((response) => {
  //   YYYYYList.value = response.rows;
  //   total.value = response.total;
  //   loading.value = false;
  // });
  for (let i = 0; i < 50; i++) {
    let item1 = {
      id: i,
      name: "电表 1-" + i,
      AAA: "0.0" + i,
      BBB: "0.0" + i,
      CCC: 0,
      stateResult: "正常",
    };
    YYYYYList.value.push(item1);
  }
  handleNodeClick(YYYYYList.value[0]);
  loading.value = false;
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    state: null,
    remark: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    delFlag: null,
  };
  proxy.resetForm("YYYYYRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加操作";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getXXXXX(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改操作";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["YYYYYRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateXXXXX(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addXXXXX(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delXXXXX(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/YYYYY/export",
    {
      ...queryParams.value,
    },
    `YYYYY_${new Date().getTime()}.xlsx`
  );
}
</script>
